// package/pages/addInvoice/addInvoice.js
let myRequest = require('../../../../common/script/myRequest.js');
let api = require('../../../../common/script/api.js');
let login = require('../../../../common/script/login');
let wxbarcode = require('../../../../utils/wxbarcode/index.js')
const utils = require('../../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      province_type: '',
      orgcode: '',
      key: '',
      date: '',
      name: '',
      code: '',
      address: '',
      phone: '',
      bank: '',
      account: '',
      email: '',
      mobilephone: '',
    },
    templateData: [],
    pop: false,
    urlType: 1
  },
  getName(e) {
    const that = this;
    that.setData({
      [`formData.name`]: e.detail.value
    })
  },
  getCode(e) {
    const that = this;
    that.setData({
      [`formData.code`]: e.detail.value
    })
  },
  getAddress(e) {
    const that = this;
    that.setData({
      [`formData.address`]: e.detail.value
    })
  },
  getPhone(e) {
    const that = this;
    that.setData({
      [`formData.phone`]: e.detail.value
    })
  },
  getBank(e) {
    const that = this;
    that.setData({
      [`formData.bank`]: e.detail.value
    })
  },
  getAccount(e) {
    const that = this;
    that.setData({
      [`formData.account`]: e.detail.value
    })
  },
  getEmail(e) {
    const that = this;
    that.setData({
      [`formData.email`]: e.detail.value
    })
  },
  getMobilephone(e) {
    const that = this;
    that.setData({
      [`formData.mobilephone`]: e.detail.value
    })
  },
  //显示模版
  history() {
    const that = this;
    that.setData({
      pop: true
    })
  },
  //添加
  addInvoice() {

    const that = this;
    if (!that.data.formData.name) {
      wx.showToast({
        title: '请填写单位名称',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!that.data.formData.email) {
      wx.showToast({
        title: '请填写邮箱',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    if (!that.data.formData.mobilephone) {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    let data = {
      token: wx.getStorageSync('token'),
      province_type: that.data.formData.province_type,
      orgcode: that.data.formData.orgcode,
      key: that.data.formData.key,
      date: that.data.formData.date,
      name: that.data.formData.name,
      code: that.data.formData.code,
      address: that.data.formData.address,
      phone: that.data.formData.phone,
      bank: that.data.formData.bank,
      account: that.data.formData.account,
      email: that.data.formData.email,
      mobilephone: that.data.formData.mobilephone,
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.qyCreate, data).then(res => {
      wx.hideLoading();

      if (res.data.status == 1) {
        wx.showToast({
          title: res.data.msg,
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () {
          if (that.data.urlType == 1) {
            let pages = getCurrentPages(); //获取小程序页面栈
            let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
            beforePage.getData(); //触发上个页面自定义的shuaxin方法
            wx.navigateBack({
              delta: 1
            })
          } else {
            wx.redirectTo({
              url: `/package/pages/invoice/invoice`,
            })
          }

        }, 2000)

      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {}
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    login.checkLogin().then(res => {
      that.getData(options)
      that.getTemplate()
    }).catch(err => {
      app.appCallBack = (e) => {
        that.getData(options)
        that.getTemplate()
      }
    })


  },
  templateBtn(e) {
    const that = this;
    let itemData = that.data.templateData[e.currentTarget.dataset.index]
    that.setData({
      [`formData.name`]: itemData.name,
      [`formData.code`]: itemData.code,
      [`formData.address`]: itemData.address,
      [`formData.phone`]: itemData.phone,
      [`formData.bank`]: itemData.bank,
      [`formData.account`]: itemData.account,
      [`formData.email`]: itemData.email,
      [`formData.mobilephone`]: itemData.mobilephone,
      pop: false
    })
  },
  popRem() {
    const that = this;
    that.setData({
      pop: false
    })
  },
  getTemplate() {
    const that = this;
    let data = {
      token: wx.getStorageSync('token'),
    }
    myRequest.EasyHttp.post(api.reqUrls.getOldTem, data).then(res => {
      if (res.data.status == 1) {
        that.setData({
          templateData: res.data.result
        })
      } else {
        wx.showModal({
          content: res.data.msg,
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(e) {}
        })
      }
    }).catch(err => {
      wx.showModal({
        content: err.toString(),
        showCancel: false,
        confirmText: '我知道了',
        confirmColor: '#ff5151',
        success(e) {}
      })
    })
  },
  getData(options) {
    console.log(options, 55555)
    const that = this;
    if (options.orgcode) {
      let data = {
        province_type: options.province_type,
        orgcode: options.orgcode,
        key: decodeURIComponent(options.key),
        date: options.date,
        token: wx.getStorageSync('token'),
      }
      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.invoiceCheck, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          that.setData({
            [`formData.orgname`]: res.data.result.data.orgname,
            [`formData.orgcode`]: res.data.result.data.orgcode,
            [`formData.key`]: res.data.result.data.key,
            [`formData.date`]: res.data.result.data.date,
            [`formData.province_type`]: options.province_type,
            urlType: 1
          })

        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#ff5151',
            success(res) {
              if (res.confirm) {

                wx.navigateBack({
                  delta: 1
                })
              }
            }
          })
        }
      }).catch(err => {
        console.log(888888)
        wx.showModal({
          showCancel: false,
          content: err.toString(),
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })

      })
    } else {
      let scene = decodeURIComponent(options.q)
      const url = scene.substring(scene.indexOf('?') + 1)
      const params = url.split('&')
        .reduce(function (acc, param) {
          const [key, value] = param.split('=');
          acc[key] = decodeURIComponent(value);
          return acc;
        }, {});
      let province_type = params['m'] ? 1 : 2;
      let orgcode = params['o'];
      let key = params['k'];
      let date = params['d'];
      let data = {
        province_type: province_type,
        orgcode: orgcode,
        key: key,
        date: date,
        token: wx.getStorageSync('token'),
      }

      wx.showLoading({
        title: '加载中...',
      })
      myRequest.EasyHttp.post(api.reqUrls.invoiceCheck, data).then(res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          that.setData({
            [`formData.orgname`]: res.data.result.data.orgname,
            [`formData.orgcode`]: res.data.result.data.orgcode,
            [`formData.key`]: res.data.result.data.key,
            [`formData.date`]: res.data.result.data.date,
            [`formData.province_type`]: province_type,
            urlType: 2
          })

        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            confirmText: '我知道了',
            confirmColor: '#ff5151',
            success(e) {}
          })
        }
      }).catch(err => {
        console.log(99999)
        wx.showModal({
          content: err.toString(),
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#ff5151',
          success(res) {
            wx.redirectTo({
              url: `/package/pages/invoice/invoice`,
            })
          }
        })

      })

    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})